package entity;

import image.Image;
import image.ImageCache;

import java.awt.Graphics;
import java.awt.Rectangle;


/**
 * An entity represents any element that appears in the game.
 * 
 * @author Neil Cowzer
 */
public class Entity{
	/** The current x-positon of this entity */
	protected int x;
	//@ invariant -10 <= x &&

	/** The current y-positon of this entity */
	protected int y;
	//@ invariant 0 <= y
	
	/** The current sprite that represents this entity*/
	protected /*@ non_null @*/ Image img;
	
	private int cycle;
	/** The location of the image. */
	private String ref;
	
	/**
	 * Construct a entity based on a sprite image and a location.
	 * 
	 * @param ref The reference to the image to be displayed for this entity
 	 * @param x The initial x location of this entity
	 * @param y The initial y location of this entity
	 */
	/*@ requires ref != null && 0 <= x && 0 <= y;
	 *  assignable img && x && y;
	 *  ensures true;
	 */
	public Entity(String ref,int x,int y) {
		this.img = ImageCache.getCache().getSprite(ref);
		this.x = x;
		this.y = y;
		this.ref = ref;
	}
	
	/**
	 * Draw this entity
	 * 
	 * @param g The graphics component to draw
	 */
	public void draw(Graphics g) {
		img.draw(g,(int) x,(int) y);
//		if (sprite == null){
//			System.out.println("null??");
//		}
	}
	
	/** Return the location of the image. */
	public String getRef(){
		return ref;
	}
	
	/** Retrieve the current x-coordinate of this entity */
	public /*@ pure non_null @*/ int getX(){
		return x;
	}
	
	/** Retrieve the current x-coordinate of this entity */
	public /*@ pure non_null @*/ int getY(){
		return y;
	}
	
	/** Retrieve the image of the entity */
	public /*@ pure non_null @*/ Image getImage(){
		return img;
	}
	
	/**
	 * Retrieve the image bounds.
	 * 
	 * @return a rectangle giving the maximum proportions of the entity.
	 */
	//@	\result != null
	public Rectangle getBounds(){
		return new Rectangle(x, y, img.getWidth(), img.getHeight());
	}
}
